/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.io;

import java.io.IOException;

/**
 * Enables the write, in particular append, access to a file, providing methods
 * to append data to that file.
 * 
 * @author Marco Tamburelli
 */
public interface Appender
{
	/**
	 * Returns the position of the cursor within the file.
	 * 
	 * Note that at the initial state the cursor should be positioned at the
	 * beginning of the file, at the 0 position.
	 * 
	 * @return
	 * @throws IOException
	 */
	long getCursor() throws IOException;

	/**
	 * appends a string to the end of the file.
	 * 
	 * @param string
	 * @throws IOException
	 */
	void appendString(String string) throws IOException;

	/**
	 * appends a string on byte array representation to the end of the file.
	 * 
	 * @param bytes
	 * @throws IOException
	 */
	void appendStringAsBytes(byte[] bytes) throws IOException;

	/**
	 * appends a double to the end of the file.
	 * 
	 * @param d
	 * @throws IOException
	 */
	void appendDouble(double d) throws IOException;

	/**
	 * appends a float to the end of the file.
	 * 
	 * @param f
	 * @throws IOException
	 */
	void appendFloat(float f) throws IOException;

	/**
	 * appends a long to the end of the file.
	 * 
	 * @param l
	 * @throws IOException
	 */
	void appendLong(long l) throws IOException;

	/**
	 * appends an integer to the end of the file.
	 * 
	 * @param i
	 * @throws IOException
	 */
	void appendInt(int i) throws IOException;

	void appendShort(short s) throws IOException;

	void appendChar(char c) throws IOException;

	void appendByte(byte b) throws IOException;

	void appendBoolean(boolean b) throws IOException;

	void appendType(byte type) throws IOException;

	/**
	 * Prepares a portion of the file, in the adjacent position of the last
	 * inserted elements, to be written, and set a limit for other appending
	 * concurrent processes. Other processes should invoke the same method (
	 * {@link #reserve(int)} in order to append a sequence of elements, and to
	 * set a new limit. <br>
	 * <br>
	 * Invoking this method will move the cursor position to the current limit
	 * of resource. <br>
	 * <br>
	 * WARNING: this is a low level function, the number of wrote bytes is not
	 * checked, so the reserved quantity can be exceeded. Clients should have
	 * care not to insert a greater amount of bytes.
	 * 
	 * @param length the number of bytes reserved for append.
	 * @throws IOException
	 */
	void reserve(int length) throws IOException;
}
